package com.ramos.kuaixiao.ui;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.githang.statusbar.StatusBarCompat;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.adapter.UnSubmitOrderAdapter;
import com.ramos.kuaixiao.db.UnSubmitOrderDB;
import com.ramos.kuaixiao.entity.FindOrderDetail;
import com.ramos.kuaixiao.entity.OrderDetail;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

public class UnSubmitOrderActivity extends BaseActivity implements View.OnClickListener {

    private final static String TAG = "UnSubmitOrderActivity";
    private TextView tv_aboutus;
    private Context mContext;
    ListView lv_unsubmit_order;
    public final static int UPLOAD_SUCCESS = 100;
    public final static int UPLOAD_FAIL = 101;
    LinearLayout title_reback;
    LinearLayout ll_unfinished_none;
    List<FindOrderDetail> list = new ArrayList<>();
    private UnSubmitOrderAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unsubmit_orderl);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.title), false);
        mContext = this;
        tv_aboutus = (TextView) findViewById(R.id.title_text);
        tv_aboutus.setText("未完成订单");
        title_reback = (LinearLayout) findViewById(R.id.title_reback);
        title_reback.setOnClickListener(this);
        ll_unfinished_none = (LinearLayout) findViewById(R.id.ll_unfinished_none);
        lv_unsubmit_order = (ListView) findViewById(R.id.lv_unsubmit_order);
    }

    @Override
    protected  void onResume(){
        super.onResume();
        UnSubmitOrderDB db = new UnSubmitOrderDB();
        list = db.findAllUnSubmitOrder();
        if (list == null || list.size() == 0) {
            ll_unfinished_none.setVisibility(View.VISIBLE);
            lv_unsubmit_order.setVisibility(View.GONE);
        } else {
            ll_unfinished_none.setVisibility(View.GONE);
            lv_unsubmit_order.setVisibility(View.VISIBLE);
            mAdapter = new UnSubmitOrderAdapter(this, list);
            lv_unsubmit_order.setAdapter(mAdapter);
            lv_unsubmit_order.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d(TAG, "onItemClick:"+position);
                    FindOrderDetail order = (FindOrderDetail) parent.getAdapter().getItem(position);
                    Intent intent = new Intent(mContext, UnSubmitOrderDetailActivity.class);
                    intent.putExtra("order", order);
                    mContext.startActivity(intent);
                }
            });
            Product product = new Product(list);
            product.executeOnExecutor(Executors.newCachedThreadPool());
        }
    }

    class Product extends AsyncTask<String, String, String> {
        List<FindOrderDetail> list;
        List<OrderDetail> detaillist;
        List<String> orderImageList;

        public Product(List<FindOrderDetail> type) {
            list = type;
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                for (int i = 0; i < list.size(); i++) {
                    detaillist = JSONArray.parseArray(list.get(i).getOrderDetailList(), OrderDetail.class);
                    Log.d(TAG, "detaillist" + detaillist.toString());
                    orderImageList = new ArrayList<>();
                    if (detaillist != null && detaillist.size() > 0) {
                        for (OrderDetail orderDetail : detaillist) {
                            String imgURL = Utils.showImg(orderDetail.getProductInfoId(), orderDetail.getProductBarCode());
                            if (!StringUtil.isEmpty(imgURL)) {
                                orderImageList.add(imgURL);
                            }
                        }
                    }
                    list.get(i).setOrderImageList(orderImageList);
                    Log.d(TAG, "orderImageList " + i + " " + orderImageList.size());
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onPostExecute " + list.size());
            //mAdapter.refresh(list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_reback:
                finish();
                break;
        }
    }
}


